package com.training.admin.entity.search;

import java.util.Date;
import java.util.Map;

import javax.persistence.FetchType;
import javax.persistence.ManyToOne;

import org.hibernate.annotations.Type;
import org.springframework.format.annotation.DateTimeFormat;

import com.demo2do.core.persistence.support.QueryBuilder;
import com.demo2do.core.persistence.support.QueryFilter;
import com.demo2do.core.persistence.support.QueryOrder;

/**
 *
 * @author sjh
 *
 */
public class ProjectDataSearch implements QueryBuilder{

	  /**
     * 项目名称
     */
    private String name;
    
    /**
     * 项目编号
     */
    private String code;
    
    /**
     * 一级分类
     */
    private Long firstCategory;
    
    /**
     * 二级分类
     */
    private Long secondCategory;
    
    /**
     * 期数
     */
    private Integer phaseCount;
    
    /**
     * 项目负责人
     * 
     */
    private String projectLead;

    /**
     * 部门
     */
    @ManyToOne(fetch = FetchType.LAZY, optional = true)
    private Long department;

    
    /**
     * 开始日期从
     */
	
	  @DateTimeFormat(pattern = "yyyy-MM-dd")
	  @Type(type = "date")
	  private Date beginDateFrom;
	 
    
    /**
     * 开始日期至
     */
    @DateTimeFormat(pattern = "yyyy-MM-dd")
    @Type(type = "date")
    private Date beginDateTo;
    
    
    /**
     * 结束日期从
     */
    @DateTimeFormat(pattern = "yyyy-MM-dd")
    @Type(type = "date")
    private Date endDateFrom;
    
    /**
     * 结束日期至
     */
    @DateTimeFormat(pattern = "yyyy-MM-dd")
    @Type(type = "date")
    private Date endDateTo;
    
    
    /**
     * 创建日期
     */
    @DateTimeFormat(pattern = "yyyy-MM-dd")
    private Date createTime;
    

    private QueryFilter filter;
    
    private QueryOrder order;

    
    /**
     * The default constructor
     */
    public ProjectDataSearch() {
        order = new QueryOrder();
    }
    
    
    /**
     * 
     * @param departmentId
     * @return
     */
    public ProjectDataSearch setDepartmentIds(Long departmentId) {
    	
    	this.department = departmentId;
    	return this;
    	
    }
    
    /* (non-Javadoc)
     * @see com.demo2do.core.persistence.support.QueryBuilder#getPreparedFilter(boolean)
     */
    public QueryFilter getPreparedFilter(boolean refresh) {
    	
        if (filter == null || refresh) {
        	
            filter = new QueryFilter().addLike("code", code)
					.addLike("name", name)
					.addEquals("firstCategory.id", firstCategory)
					.addEquals("secondCategory.id", secondCategory)
					.addDateTimeBetween("beginDate", beginDateFrom, beginDateTo)
					.addDateTimeBetween("endDate", endDateFrom, endDateTo)
					.addEquals("phaseCount", phaseCount)
					.addEquals("projectLead", projectLead)
					.addEquals("department.id", department);
            
            
        }
        
        return filter;
        
    }
    
 
	/*
     * (non-Javadoc)
     * @see com.demo2do.core.persistence.support.QueryBuilder#buildSentenceTemplate()
     */
    public String buildSentenceTemplate() {
        String sentence = "FROM Project WHERE disabled = false " + getPreparedFilter(false).clauseSentence() + order.sentence();
     
        return sentence;
    }

    /*
     * (non-Javadoc)
     * @see com.demo2do.core.persistence.support.QueryBuilder#buildParameters()
     */
    public Map<String, Object> buildParameters() {
        return getPreparedFilter(false).parameters();
    }
    

    /**
     * 
     * @return
     */
    public QueryFilter getFilter(){
        return getPreparedFilter(false);
    }
	/**
	 * @return the order
	 */
	public QueryOrder getOrder() {
		return order;
	}


	/**
	 * @return the name
	 */
	public String getName() {
		return name;
	}


	/**
	 * @return the code
	 */
	public String getCode() {
		return code;
	}


	/**
	 * @return the firstCategory
	 */
	public Long getFirstCategory() {
		return firstCategory;
	}


	/**
	 * @return the secondCategory
	 */
	public Long getSecondCategory() {
		return secondCategory;
	}


	/**
	 * @return the phaseCount
	 */
	public Integer getPhaseCount() {
		return phaseCount;
	}


	/**
	 * @return the projectLead
	 */
	public String getProjectLead() {
		return projectLead;
	}


	/**
	 * @return the department
	 */
	public Long getDepartment() {
		return department;
	}


	/**
	 * @return the beginDateFrom
	 */
	public Date getBeginDateFrom() {
		return beginDateFrom;
	}


	/**
	 * @return the beginDateTo
	 */
	public Date getBeginDateTo() {
		return beginDateTo;
	}


	/**
	 * @return the endDateFrom
	 */
	public Date getEndDateFrom() {
		return endDateFrom;
	}


	/**
	 * @return the endDateTo
	 */
	public Date getEndDateTo() {
		return endDateTo;
	}


	/**
	 * @return the createTime
	 */
	public Date getCreateTime() {
		return createTime;
	}


	/**
	 * @param name the name to set
	 */
	public void setName(String name) {
		this.name = name;
	}


	/**
	 * @param code the code to set
	 */
	public void setCode(String code) {
		this.code = code;
	}


	/**
	 * @param firstCategory the firstCategory to set
	 */
	public void setFirstCategory(Long firstCategory) {
		this.firstCategory = firstCategory;
	}


	/**
	 * @param secondCategory the secondCategory to set
	 */
	public void setSecondCategory(Long secondCategory) {
		this.secondCategory = secondCategory;
	}


	/**
	 * @param phaseCount the phaseCount to set
	 */
	public void setPhaseCount(Integer phaseCount) {
		this.phaseCount = phaseCount;
	}


	/**
	 * @param projectLead the projectLead to set
	 */
	public void setProjectLead(String projectLead) {
		this.projectLead = projectLead;
	}


	/**
	 * @param department the department to set
	 */
	public void setDepartment(Long department) {
		this.department = department;
	}


	/**
	 * @param beginDateFrom the beginDateFrom to set
	 */
	public void setBeginDateFrom(Date beginDateFrom) {
		this.beginDateFrom = beginDateFrom;
	}


	/**
	 * @param beginDateTo the beginDateTo to set
	 */
	public void setBeginDateTo(Date beginDateTo) {
		this.beginDateTo = beginDateTo;
	}


	/**
	 * @param endDateFrom the endDateFrom to set
	 */
	public void setEndDateFrom(Date endDateFrom) {
		this.endDateFrom = endDateFrom;
	}


	/**
	 * @param endDateTo the endDateTo to set
	 */
	public void setEndDateTo(Date endDateTo) {
		this.endDateTo = endDateTo;
	}


	/**
	 * @param createTime the createTime to set
	 */
	public void setCreateTime(Date createTime) {
		this.createTime = createTime;
	}

	
	
}
